package com.juntuo.controller;

import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import com.juntuo.Enums.ParkStatus;
import com.juntuo.entity.Park;
import com.juntuo.service.ParkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 对片区 街道 泊位等进行管理
 * @author chenxj
 * @create 2017/03/03 11:04
 */
@Controller
@RequestMapping("/park")
public class ParkController {

    @Autowired
    private ParkService parkService;

    @RequestMapping("/index")
    public String toIndex(){
        return "/park/index";
    }

    @RequestMapping("/page")
    @ResponseBody
    public PageInfo<Map<String,Object>> getAllList(int pageNumber, int pageSize, @RequestParam(defaultValue = "0")int areaId,  @RequestParam(defaultValue = "0")int streetId, String number){
        return parkService.findPagedPark(pageNumber,pageSize,areaId,streetId,number);
    }

    @RequestMapping("/list/unused")
    @ResponseBody
    public List<Park> getParkListByStreet(int streetId){
        Park park = new Park();
        park.setStreetId(streetId);
        park.setStatus(ParkStatus.WAITING_FOR_BIND.getCode());
        List<Park> parks=parkService.select(park);
        return parks;
    }


    @RequestMapping("/save")
    @ResponseBody
    public boolean addOne(@RequestParam(defaultValue = "0")int id,String number,int streetId){
        if(id==0){
            parkService.addPark(number,streetId);
        }else{
            Park park = new Park();
            park.setId(id);
            if(StringUtil.isNotEmpty(number)){
                park.setNumber(number);
            }
            if(streetId>0){
                park.setStreetId(streetId);
            }
            parkService.updateByPrimaryKeySelective(park);
        }

        return true;
    }


    @RequestMapping("/delete/{id}")
    @ResponseBody
    public int deleteOne(@PathVariable int id){
        Park park = new Park();
        park.setId(id);
        parkService.delete(park);
        return 1;
    }


}
